package cn.com.surker.system.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.system.dao.SysUserPositionDao;
import cn.com.surker.system.entity.SysUserPosition;
import cn.com.surker.system.service.SysUserPositionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luotao
 * @date 2023/3/21
 */
@Service
public class SysUserPositionServiceImpl extends ServiceImpl<SysUserPositionDao, SysUserPosition> implements SysUserPositionService {

	@Resource
	private SysUserPositionDao sysUserPositionDao;

	@Override
	public SysUserPosition getUserDefault(Long userId) {
		if(userId == null){
			return null;
		}
		QueryWrapper<SysUserPosition> qw = new QueryWrapper<>();
		qw.eq("uid",userId);
		qw.eq("is_default",1);
		qw.eq("status", StateEnum.ENABLED.getCode());
		List<SysUserPosition> userDefaultPositions = sysUserPositionDao.selectList(qw);
		if(userDefaultPositions == null || userDefaultPositions.isEmpty()){
			//  没有设置默认岗位,去第一个岗位
			QueryWrapper<SysUserPosition> qw2 = new QueryWrapper<>();
			qw2.eq("uid",userId);
			qw2.eq("status", StateEnum.ENABLED.getCode());
			qw2.orderByAsc("id");
			List<SysUserPosition> userPositions = sysUserPositionDao.selectList(qw2);
			if(userPositions != null && !userPositions.isEmpty()){
				SysUserPosition userPosition = userPositions.get(0);
				return userPosition;
			}else {
				return null;
			}
		}
		SysUserPosition userPosition = userDefaultPositions.get(0);
		return userPosition;
	}

	@Override
	public List<SysUserPosition> findUserPosList(Long userId) {
		if(userId == null){
			return null;
		}
		QueryWrapper<SysUserPosition> qw2 = new QueryWrapper<>();
		qw2.eq("uid",userId);
		qw2.eq("status", StateEnum.ENABLED.getCode());
		qw2.orderByAsc("id");
		List<SysUserPosition> userPositions = sysUserPositionDao.selectList(qw2);
		return userPositions;
	}

	@Override
	public List<SysUserPosition> findUserPosList(Long userId, List<Long> deptIdList) {
		if(userId == null){
			return null;
		}
		QueryWrapper<SysUserPosition> qw2 = new QueryWrapper<>();
		qw2.eq("status", StateEnum.ENABLED.getCode());
		qw2.eq("uid",userId);
		if(deptIdList != null && !deptIdList.isEmpty()){
			qw2.in("org_id",deptIdList);
		}
		qw2.orderByAsc("id");
		List<SysUserPosition> userPositions = sysUserPositionDao.selectList(qw2);
		return userPositions;
	}
}
